<?php
session_start();

//$email = $_SESSION['email'];
$email = 'test@pdn.ac.lk';

$connetion = mysql_connect('localhost', 'root', 'thMOziJu') or die('Error in DB');
$db = mysql_select_db('supervisor_all_system') or die('Érror in selecting db');

$default_combo = "";

if ($_GET['get_times']) {
	$day = $_GET['day'];
	//times will hold the particular time values for the exam
	$times = array();
	
	//Get values of times for the given day
	$query = "select sub_start, sub_end from sase_subject where sub_day = '$day'";
	$result = mysql_query($query);
	
	//put them in to the times array
	while ($row = mysql_fetch_assoc($result)) {
		$times[] = $row['sub_start'] . ' to ' . $row['sub_end'];	
	}
	
	$times = array_unique($times);
	sort($times);
	
	//generate option box optins html
	$str_times = "";
	foreach ($times as $v) {
        $str_times =  $str_times."<option value=\"".$v."\">".$v."</option>";
    }
}
elseif ($_GET['get_list']) {
	//Load the combo box, default value is last selected
	$default_combo = $_GET['day'];
	
	$query = 'select * from Exam';
	$result = mysql_query($query);

	$dates = array();
	
	while ($row = mysql_fetch_assoc($result)){
		$tmp = explode(" ", $row['Start_Time']);
		$dates[] = $tmp[0];
	}
	
	$dates = array_unique($dates);
	sort($dates);
	
	$str_dates = "<option value=\"".$default_combo."\">".$default_combo."</option>";

	foreach ($dates as $v) {
		$str_dates =  $str_dates."<option value=\"".$v."\">".$v."</option>";
	}
	
	$query = "select * from Exam where Start_Time > '".$default_combo." 00:00:00' and Start_Time < '".$default_combo." 23:59:59'";
	$result = mysql_query($query);
	
	$sub_array = array();
	
	while ($row = mysql_fetch_assoc($result)){
        $sub_array[] = $row['Sub_Code'];
    }
	
	$str_exams = "";
	
	foreach ($sub_array as $v) {
        $str_exams =  $str_exams."<option value=\"".$v."\">".$v."</option>";
    }
	
}
elseif ($_GET['accept']){
	//Add to database
	$tmp = explode(' ', $_GET['select_exam']);
	$sub_code = strtolower($tmp[0]);
	
	$query = "insert into supervises_exam (Email, Sub_Code) values ('$email', '$sub_code')";
	//echo $query;
	$result = mysql_query($query);
	
	//Load the combo box, default value is last selected
	$default_combo = $_GET['day'];
	
	$query = 'select * from Exam';
	$result = mysql_query($query);

	$dates = array();
	
	while ($row = mysql_fetch_assoc($result)){
		$tmp = explode(" ", $row['Start_Time']);
		$dates[] = $tmp[0];
	}
	
	$dates = array_unique($dates);
	sort($dates);
	
	$str_dates = "<option value=\"".$default_combo."\">".$default_combo."</option>";

	foreach ($dates as $v) {
		$str_dates =  $str_dates."<option value=\"".$v."\">".$v."</option>";
	}
	
	$query = "select * from Exam where Start_Time > '".$default_combo." 00:00:00' and Start_Time < '".$default_combo." 23:59:59'";
	$result = mysql_query($query);
	
	$sub_array = array();
	
	while ($row = mysql_fetch_assoc($result)){
        $sub_array[] = $row['Sub_Code'];
    }
	
	$str_exams = "";
	
	foreach ($sub_array as $v) {
        $str_exams =  $str_exams."<option value=\"".$v."\">".$v."</option>";
    }
}
else {
	//Just load the first combo box
	$query = 'select * from Exam';
	$result = mysql_query($query);

	$dates = array();
	
	while ($row = mysql_fetch_assoc($result)){
		$tmp = explode(" ", $row['Start_Time']);
		$dates[] = $tmp[0];
	}
	
	$dates = array_unique($dates);
	sort ($dates);
	
	$str_dates = "";

	foreach ($dates as $v) {
		$str_dates =  $str_dates."<option value=\"".$v."\">".$v."</option>";
	}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;	
}
</style>
</head>

<body>
<div class="container">
	<center><h1>Search Exams</h1></center>
	<div class="search_box">
	    <form action="search.php" method="get">
            <select name="day" size="1">
                <?php echo $str_dates; ?>
            </select>
            <input name="action" type="hidden" id="action" value="search" />
<input type="submit" value="get times" name="get_times" />
	    </form>
    </div>
    
    <div class="search_box">
	    <form action="search.php" method="get">
            <select name="day" size="1">
                <?php echo $str_times; ?>
            </select>
            <input type="hidden" value="<?php echo $select_date; ?>" name="day" />
            <input name="action" type="hidden" id="action" value="search" />
            <input type="submit" value="get list" name="get_list" />
	    </form>
    </div>
    
    <div class="search_box">
    	<form action="search.php" method="get">
            <select name="select_exam" size="1">
                <?php echo $str_dates; ?>
            </select>
            <input name="action" type="hidden" id="action" value="search" />
            <input type="submit" value="accept" name="accept" />
		</form>
    </div>
    
    <div class="search_box">
    	<center><a href="index.php">Back to Home</a></center>
    </div>
    
    <div style="padding:15px;"></div>
</div>

</body>
</html>